<?php

namespace App\Admin\Controllers;

use App\Services\SettingService;
use Illuminate\Http\Request;
use Slowlyo\OwlAdmin\Controllers\AdminController;

class SettingController extends AdminController
{

	protected string $serviceName = SettingService::class;
    public function index()
    {
        if ($this->actionOfGetData()) return $this->response()->success(settings()->all());

        $page = $this->basePage()->body([
            $this->form(),
        ]);

        return $this->response()->success($page);
    }

    public function form()
    {
        return $this->baseForm(false)
            ->redirect('')
            ->api($this->getStorePath())
            ->initApi('/system/settings?_action=getData')
            ->body(
                amis()->Tabs()->tabs([
                    amis()->Tab()->title('AI')->body([
                        amis()->TextControl()->label('APP_KEY')->name('app_key'),
                        amis()->TextControl()->label('【普通版】应用ID')->name('plus_app_id'),
                        amis()->TextControl()->label('【高级版】应用ID')->name('max_app_id'),
                    ]),
                    amis()->Tab()->title('其他')->body([
                        amis()->NumberControl()->label('日志有效天数')->name('log_expire_days'),
                    ]),
                ])
            );
    }

    public function store(Request $request)
    {
        $data = $request->only([
            'app_key',
            'plus_app_id',
            'max_app_id',
            'log_expire_days',
        ]);
        return settings()->adminSetMany($data);
    }

	public function getSetting()
	{
		return $this->response()->success($this->service->getSetting());
	}

	public function setSetting()
	{
		return $this->response()->success($this->service->setSetting());
	}
}
